package com.example.smartall.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.smartall.R;
import com.example.smartall.api.Api;
import com.example.smartall.api.ApiConfig;
import com.example.smartall.api.NetCallBack;
import com.example.smartall.entity.NewsBean;
import com.example.smartall.ui.NewsDetailsActivity;

import org.w3c.dom.Text;

import java.util.ArrayList;
import java.util.List;
//新闻列表适配器
public class NewsListAdapter extends BaseAdapter {

    private Context context;
    private List<NewsBean.RowsBean> newsBeans ;
    //显示的新闻类别
    private int witch;

    public NewsListAdapter(Context context, List<NewsBean.RowsBean> newsBeans, int witch) {
        this.context = context;
        this.newsBeans = newsBeans;
        this.witch = witch;
    }

    public NewsListAdapter(Context context, List<NewsBean.RowsBean> newsBeans) {
        this.context = context;
        this.newsBeans = newsBeans;
    }

    public NewsListAdapter(Context context) {
        this.context = context;
    }

    public void setNewsBeans(List<NewsBean.RowsBean> newsBeans) {
//        if (this.newsBeans != null && this.newsBeans.size() != 0){
//            if (!this.newsBeans.containsAll(newsBeans)){
//                this.newsBeans.clear();
//            }
//        }
        this.newsBeans = newsBeans;
        this.notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return newsBeans == null ? 0 : newsBeans.size();
    }

    @Override
    public Object getItem(int i) {
        return newsBeans.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View convertView, ViewGroup viewGroup) {
        ViewHolder viewHolder;
        if (convertView == null){
            viewHolder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.common_list_item,null);
            viewHolder.img = (ImageView) convertView.findViewById(R.id.img);
            viewHolder.title = (TextView) convertView.findViewById(R.id.title);
            viewHolder.subtitle = (TextView) convertView.findViewById(R.id.subtitle);
            viewHolder.content = convertView.findViewById(R.id.content);
            convertView.setTag(viewHolder);
        }else{
            viewHolder = (ViewHolder) convertView.getTag();
        }
//        if (witch == Integer.parseInt(newsBeans.get(i).getType())){
//            Glide.with(context).load(ApiConfig.BASE_URL + newsBeans.get(i).getCover()).into(viewHolder.img);
//            viewHolder.title.setText(newsBeans.get(i).getTitle());
//            viewHolder.subtitle.setText(newsBeans.get(i).getSubTitle());
//        }
        Glide.with(context).load(ApiConfig.BASE_URL + newsBeans.get(i).getCover()).into(viewHolder.img);
        viewHolder.title.setText(newsBeans.get(i).getTitle());
        viewHolder.subtitle.setText(newsBeans.get(i).getSubTitle());
        viewHolder.content.setText("发表时间："+newsBeans.get(i).getPublishDate()+"\t" +
                "观看人数："+newsBeans.get(i).getReadNum() +"\t点赞数："+newsBeans.get(i).getLikeNum());

        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(context,NewsDetailsActivity.class);
                intent.putExtra("id",newsBeans.get(i).getId());
                intent.putExtra("title",newsBeans.get(i).getTitle());
                context.startActivity(intent);
            }
        });
        return convertView;
    }

    class ViewHolder{
        private ImageView img;
        private TextView title;
        private TextView subtitle;
        private TextView content;
    }
}
